package com.itheima.reggie.controller;

import cn.hutool.core.util.RandomUtil;
import com.itheima.reggie.common.Constant;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.common.SmsTemplate;
import com.itheima.reggie.domain.User;
import com.itheima.reggie.service.UserService;
import org.apache.tomcat.util.bcel.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping(value = "/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private HttpSession session;
    @Autowired
    private SmsTemplate smsTemplate;
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping(value = "/sendMsg",name = "发送验证码")
    public ResultInfo sendMsg(@RequestBody Map<String,String> map){
        String phone = map.get("phone");
        System.out.println(phone);
        String code = "123";//此处的验证码写死了,后用阿里云的短信验证码服务替换
        //生成一个六位数的随机验证码
        //String code = RandomUtil.randomNumbers(6);
        //System.out.println(code);
        //发送验证码到手机号
        //smsTemplate.sendSms(phone,code);
        //将验证码存入域对象中
        //session.setAttribute(Constant.USER_PHONE_CODE+phone,code);
        //redis存入验证码
        redisTemplate.opsForValue().set(Constant.USER_PHONE_CODE+phone,code,5, TimeUnit.MINUTES);
        return ResultInfo.success(null);
    }

    @PostMapping(value = "/login",name = "用户登录")
    public ResultInfo login(@RequestBody Map<String,String> map){
        //获取用户输入的验证码和手机号
        String phone = map.get("phone");
        String code = map.get("code");
        //从session中获取验证码,
        //String dbCode = (String) session.getAttribute(Constant.USER_PHONE_CODE + phone);
        //redis中获取验证码
        String dbCode = (String) redisTemplate.opsForValue().get(Constant.USER_PHONE_CODE + phone);
        //判断用户输入的验证码是否正确
        if (!dbCode.equals(code)){
            throw new CustomException("验证码错误");
        }
        //验证码正确,查询用户是否注册
        User user = userService.findByPhone(phone);
        if (user==null){
            user = new User();
            user.setPhone(phone);
            user.setStatus(1);
            userService.add(user);
        }
        //清除验证码
        //session.removeAttribute(Constant.USER_PHONE_CODE+phone);
        //redis清除验证码
        redisTemplate.delete(Constant.USER_PHONE_CODE+phone);
        //将用户保存到域对象中
        session.setAttribute(Constant.SESSION_USER,user);
        return ResultInfo.success(user);
    }

    @PostMapping(value = "/logout")
    public ResultInfo logout(){
        //清除缓存的user
        session.removeAttribute(Constant.SESSION_USER);
        return ResultInfo.success("退出成功");
    }
}
